package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.listener.ItemClickListener;
import com.yoomiito.app.model.bean.BannerAndIconInfo;
import com.yoomiito.app.utils.ImageLoader;

import java.util.List;

/**
 * 首页---tab的Adapter
 */
public class HomeIconAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<BannerAndIconInfo> datas;
    private ItemClickListener mItemClickListener;

    public HomeIconAdapter(Context context, LayoutHelper layoutHelper, List<BannerAndIconInfo> datas) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_tab, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        BannerAndIconInfo info = datas.get(position);
        itemViewHolder.tabTv.setText(info.getTitle());
        ImageLoader.getInstance().into(mContext, info.getImage_url(), itemViewHolder.tabIv);
        itemViewHolder.tabIv.setOnClickListener(v -> {
            if (mItemClickListener != null) mItemClickListener.onItemClick(position);
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        public ImageView tabIv;
        public TextView tabTv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tabIv = itemView.findViewById(R.id.tab_iv);
            tabTv = itemView.findViewById(R.id.tv_tab);
        }
    }
}
